﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using System.Data;
using System.Data.SqlClient;
using DBUnitility;
namespace DAL
{
    public class FileNameAddressService
    {
        /// <summary>
        /// 获取所有的文件信息
        /// </summary>
        /// <returns></returns>
        public List<FileNameAddress> GetAllFileName()
        {
            string sql = "select FileName,ID from FileNameAddress";
            List<FileNameAddress> list = new List<FileNameAddress>();
            SqlDataReader objReader = SQLHelper.GetReader(sql);
            while(objReader.Read())
            {
                list.Add(new FileNameAddress()
                {
                    FileName = objReader["FileName"].ToString(),
                    ID =Convert.ToInt32(objReader["ID"])
                });
            }
            objReader.Close();
            return list;
        }
        /// <summary>
        /// 按ID号查询文件信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public FileNameAddress GetFileNameById(string id)
        {
            FileNameAddress objFileNameAddress =null;
            string sql = "select FileName,ID from FileNameAddress where ID ="+ id;
            SqlDataReader objReader = SQLHelper.GetReader(sql);
            if (objReader.Read())
            {
                objFileNameAddress = new FileNameAddress()
                {
                    FileName = objReader["FileName"].ToString(),
                    ID = Convert.ToInt32(objReader["ID"])
                };
            }
            objReader.Close();
            return objFileNameAddress;
        }
        /// <summary>
        /// 新增一个文件
        /// </summary>
        /// <param name="objFileNameAddress">文件信息</param>
        /// <returns>返回值为ID</returns>
        public int AddFileName(FileNameAddress objFileNameAddress)
        {
            string sql = "insert into FileNameAddress(FileName) values('{0}');select @@Identity";
            sql = string.Format(sql,objFileNameAddress.FileName);
            return Convert.ToInt32(SQLHelper.GetSingalResult(sql));
        }
        /// <summary>
        /// 修改文件信息
        /// </summary>
        /// <param name="objFileNameAddress">修改信息</param>
        /// <returns></returns>
        public int UpdateFileName(FileNameAddress objFileNameAddress)
        {
            string sql = "update FileNameAddress set FileName ='{0}' where ID = {1}";
            sql = string.Format(sql,objFileNameAddress.FileName,objFileNameAddress.ID);
            return SQLHelper.Update(sql);
        }
        /// <summary>
        /// 删除文件信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteFileName(string id)
        {
            //string sql = "delete from FileNameAddress where ID =" + id;
            string sql = "delete from FileNameAddress where 1 =1 ";
            if (id.Trim().Length != 0) sql += "and ID =" + id;
            return SQLHelper.Update(sql);
        }
    }
}
